package com.mach.platform.service;


import com.google.common.collect.Lists;
import com.mach.platform.domain.CoilingQualityWetProducts;
import com.mach.platform.domain.PitcherAutonomy;
import com.mach.platform.domain.ProductionState;
import com.mach.platform.repository.PitcherAutonomyRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class PitcherAutonomyServiceImpl implements BaseService<PitcherAutonomy> {

    @Autowired
    private PitcherAutonomyRepo pitcherAutonomyRepo;
    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Override
    public PitcherAutonomy save(PitcherAutonomy pitcherAutonomy) {
        return null;
    }

    @Override
    public List<PitcherAutonomy> insertObjInBatch(List<PitcherAutonomy> list) {
        return null;
    }

    @Override
    public PitcherAutonomy update(PitcherAutonomy pitcherAutonomy) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return pitcherAutonomyRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<PitcherAutonomy> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (ObjectUtils.allNotNull(startTime, endTime)) {
                LocalDateTime ldtStartTime = LocalDateTime.parse(startTime.toString(), df);
                LocalDateTime ldtEndTime = LocalDateTime.parse(endTime.toString(), df);
                Page<PitcherAutonomy> result = pitcherAutonomyRepo.getListByTime(ldtStartTime, ldtEndTime, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public PitcherAutonomy info(Long id) {
        return null;
    }

    public void add(List<PitcherAutonomy> pitcherAutonomies) {
        if (pitcherAutonomies != null && pitcherAutonomies.size() > 0){
            List<PitcherAutonomy> newList = Lists.newArrayList();

            pitcherAutonomies.forEach(pitcherAutonomy -> {
                if (StringUtils.isNotBlank(pitcherAutonomy.getProductSpecification())){
                    //判断日期是否为空
                    if (pitcherAutonomy.getPitcherAutonomyTime() == null) {
                        pitcherAutonomy.setPitcherAutonomyTime(LocalDateTime.now());
                    }
                    newList.add(pitcherAutonomy);
                }
            });
            //补全课长、值班主管、领班、投手、班别
            String sectionChief = null;
            String shiftSupervisor = null;
            String foreman = null;
            String classBan = null;
            String pitcher = null;
            for (PitcherAutonomy pitcherAutonomy : pitcherAutonomies){
                if (StringUtils.isNotBlank(pitcherAutonomy.getSectionChief())){
                    sectionChief = pitcherAutonomy.getSectionChief();
                }
                if (StringUtils.isNotBlank(pitcherAutonomy.getShiftSupervisor())){
                    shiftSupervisor = pitcherAutonomy.getShiftSupervisor();
                }
                if (StringUtils.isNotBlank(pitcherAutonomy.getForeman())){
                    foreman = pitcherAutonomy.getForeman();
                }
                if (StringUtils.isNotBlank(pitcherAutonomy.getClassBan())){
                    classBan = pitcherAutonomy.getClassBan();
                }
                if (StringUtils.isNotBlank(pitcherAutonomy.getPitcher())){
                    pitcher = pitcherAutonomy.getPitcher();
                }
            }
            for (PitcherAutonomy pitcherAutonomy : pitcherAutonomies){
                pitcherAutonomy.setSectionChief(sectionChief);
                pitcherAutonomy.setShiftSupervisor(shiftSupervisor);
                pitcherAutonomy.setForeman(foreman);
                pitcherAutonomy.setClassBan(classBan);
                pitcherAutonomy.setPitcher(pitcher);
            }

            pitcherAutonomyRepo.saveAll(newList);
        }
    }

    public void update(List<PitcherAutonomy> pitcherAutonomies) {
        if (pitcherAutonomies != null && pitcherAutonomies.size() > 0){
            List<PitcherAutonomy> newList = Lists.newArrayList();
            pitcherAutonomies.forEach(pitcherAutonomy -> {
                if (StringUtils.isNotBlank(pitcherAutonomy.getProductSpecification())){
                    newList.add(pitcherAutonomy);
                }
            });
            pitcherAutonomyRepo.saveAll(newList);
        }
    }

    public List<PitcherAutonomy> getProductionStateByTime(String time, String classBan) {
        if (time != null && !time.equals("")){
            List<PitcherAutonomy> result = pitcherAutonomyRepo.getProductionStateByTime(time, classBan);
            return result;
        }
        return null;
    }
}
